<template>

  <div class="layui-fulid" id="house-login">
    <div class="layui-form" >
      <p>用户登录</p>

      <el-form :model="memberUser" status-icon :rules="rules" ref="memberUser" label-position="top" label-width="100px">
        <el-form-item prop="mobile">
          <el-input type="text"  v-model="memberUser.mobile" placeholder="请输入手机号码" autocomplete="off">
            <i slot="prefix" class="el-input__icon el-icon-user"></i>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input type="password" v-model="memberUser.password"  placeholder="请输入密码"  autocomplete="true">
            <i slot="prefix" class="el-input__icon el-icon-unlock"></i>
          </el-input>
        </el-form-item>

        <el-form-item prop="imgCode">
          <el-input type="text" v-model="memberUser.imgCode" placeholder="请输入验证码结果" style="width: 55%;float: right"  autocomplete="true">
          </el-input>
          <img :src="imgUrl" @click="getCaptcha"  style="width: 45%;float: right" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login('memberUser')">登 录</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>

</template>

<script>
  import '~/assets/static/css/login.css'
  import userApi from '@/api/user'
  import { setUser } from '@/utils/auth'

  export default {
    data(){
      //校验手机号
      var checkMobile = (rule,value,callback)=>{
        if(!value){
          return callback(new Error('请输入手机号码'));
        }
        if(!(/^1[3-9]\d{9}$/).test(value)){
          return callback(new Error('请输入正确的手机号码'));
        }
        callback();
      };
      return {
        memberUser: {
          mobile: '',
          password:'',
          imgCode:'',
          loginType:'',
          deviceInfor:'',
          redisKey:''
        },
        rules: {
          mobile: [
            { validator:checkMobile, trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6,  message: '密码长度在 6 位以上', trigger: 'blur' }
          ],
          imgCode: [
            { required: true, message: '请输入验证码结果', trigger: 'blur' },
          ]
        },
        imgUrl:""
      }
    },
    created() {
      //获取设备信息
      this.getDeviceType();
      //加载验证码
      this.getCaptcha();
    },
    methods: {
      getDeviceType() {
        if(process.browser){
          let type =window.navigator.userAgent
          let deviceInfor = type.substring(type.indexOf('(')+1,type.indexOf(')')-type.indexOf('('))
          this.memberUser.deviceInfor = deviceInfor
          var isAndroid = type.indexOf('Android') > -1 || type.indexOf('Adr') > -1 //android终端
          var isiOS = !!type.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
          if (isAndroid) {
            this.memberUser.loginType = 'ANDROID'
          }else if(isiOS){
            this.memberUser.loginType = 'IOS'
          }else{
            this.memberUser.loginType = 'PC'
          }
        }
      },
      getCaptcha(){
        userApi.getCaptcha().then(res=>{
          if(res.data.flag){
            this.imgUrl=res.data.data.imgUrl;
            this.memberUser.redisKey = res.data.data.redisKey;
          }
        })
      },
      login(memberUser){
        this.$refs[memberUser].validate((valid) => {
          if (valid) {

            userApi.login( this.memberUser ).then(res => {
              if(res.data.flag){
                this.$message({
                  message: '登录成功',
                  type: 'success'
                })
                //保存用户信息
                setUser(res.data.data.token, res.data.data.name, res.data.data.avatar)
                this.memberUser={}
                //登录成功去首页
                location.href="/";
              }else{
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                })
              }
            })
          } else {
            return false;
          }
        });
      }
    }
  }
</script>
